/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectTaskCloseController
 * 项目任务关闭
 *
 * 修改纪录
 * 2021-10-09 版本：1.0 zhangqianbo 创建。
 * @version 版本：1.0
 * @author 作者：zhangqianbo
 * 创建日期：2021-10-09
 */

package com.topscomm.mpm.controller;

import java.util.Date;
import java.util.Map;

import com.topscomm.mpm.pojo.MpmProjectTaskCloseEntity;
import com.topscomm.mpm.pojo.MpmProjectTaskFeedbackEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.web.bind.annotation.*;

import com.topscomm.mpm.controller.auto.MpmProjectTaskCloseControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: zhangqianbo
 * @date: 2021-10-09
 * @description:项目任务关闭Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectTaskClose")
public class MpmProjectTaskCloseController extends MpmProjectTaskCloseControllerAuto implements IMpmProjectTaskCloseController {
	/**
	 * @author: zhangqianbo
	 * @date: 2021-10-09
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_taskid")) {
			String value = paraMap.get("qry_taskid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and taskid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state in ({0}) ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: zhangqianbo
	 * @date: 2021-10-09
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	@PutMapping(value = "/submit")
	public ResponseResult<Object> submit(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> dataMap = paraMap;
			paraMap.put("submitdate", new Date());
			beforeSubmit(dataMap, paraMap);
			String mailTitle = "";
			if (dataMap.containsKey("maintitle")) {
				mailTitle = ConvertUtil.convertToString(dataMap.get("maintitle"));
			}
			MpmProjectTaskCloseEntity mpmProjectTaskCloseEntity = this.service.queryById(ConvertUtil.convertToString(dataMap.get("id")));
			if (mpmProjectTaskCloseEntity == null) {
				Class<MpmProjectTaskCloseEntity> entityClass = this.entityClass;
				mpmProjectTaskCloseEntity = entityClass.newInstance();
			}
			// 处于审批中和已审批的单据不能再提交审批
			if (TapSystemConst.TapApprovalBillStateEnum.Approveing == ConvertUtil.convertToInt(mpmProjectTaskCloseEntity.getState())
					|| TapSystemConst.TapApprovalBillStateEnum.Approved == ConvertUtil.convertToInt(mpmProjectTaskCloseEntity.getState()))
				return ResponseResult.error("不是退回和开立的单据不能提交审批");
			for (Object key : dataMap.keySet()) {
				String tempKey = ConvertUtil.convertToString(key);
				mpmProjectTaskCloseEntity.setValue(tempKey, dataMap.get(key));
			}
			this.service.executeSubmit(mpmProjectTaskCloseEntity, mailTitle, this.getUserBean());
			dataMap = this.service.queryMapById(ConvertUtil.convertToString(dataMap.get("id")));
			// 增加前台待审批消息提醒
			afterSubmit(dataMap, paraMap);
			return ResponseResult.ok("提交成功");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交项目任务关闭申请! ", dataMap.get("dispdeptcode"), dataMap.get("applyusername"),
				DateUtil.dateToString(new Date(), "yyyy-MM-dd"));
		dataMap.put("maintitle", mailtitle.toString());

	}

}

